Hooks
useEntityListQuery
Use this hook to fetch a list of entries. For example load a list of properties.
import { useEntityListQuery } from '@rexlabs/model-generator';
const propertiesQuery = ... // list of properties query
function Component() {
const { data, errors, status, actions } = useEntityListQuery(propertiesQuery, options);
...
}
Parameters:
| Name | Type | Description |
|---|---|---|
query | Query | null | The query to fetch |
options | {fetch?: boolean}? |
Returns: Object
| Name | Type | Description |
|---|---|---|
data | any[] | The data the query has fetched |
errors | APIError[] | Array of errors that occurred when fetching the query |
status | loading | error | loaded | |
actions | EntityActions | Any default actions for the model, plus all the custom actions defined |
pagination | Pagination | pagination is only returned when status is loaded |
useEntityQuery
Use this hook to fetch a query of a single entity. For example load a single property.
import { useEntityQuery } from '@rexlabs/model-generator';
const propertyQuery = ... // single property query
function Component() {
const { data, errors, status, actions } = useEntityQuery(propertyQuery, options);
...
}
Parameters:
| Name | Type | Description |
|---|---|---|
query | Query | The query to fetch |
options | {fetch?: boolean, throwOnError?: boolean}? |
Returns: Object
| Name | Type | Description |
|---|---|---|
data | any | The data the query has fetched |
errors | APIError[] | Array of errors that occurred when fetching the query |
status | loading | error | loaded | |
actions | EntityActions | Any default actions for the model, plus all the custom actions defined |
useModelActions
This hook lets you access all actions (default and custom) associated with a given model. An entity model would include the default CRUD actions.
import { useModelActions } from '@rexlabs/model-generator';
function Component() {
const { deleteItem, ...allOtherActions } = useModelActions(propertiesModel);
...
}
Parameters:
| Name | Type | Description |
|---|---|---|
model | Model |
Returns: Object
| Name | Type | Description |
|---|---|---|
deleteItem | ({id: string}) => Promise\<unknown> | Delete the entry with the corresponding id |
trashItem | ({id: string}) => Promise\<unknown> | Delete the entry with the corresponding id |
updateItem | ({id: string, data: any}) => Promise\<Entry> | Update a specific entry |
createItem | ({data: any}) => Promise\<Entry> | Creates a new entry |
fetchItem | ({id: string}) => Promise\<Entry> | Fetches a specific entry |
fetchList | () => Promise\<unknown> | Fetches all entries of a model |
fetchList | () => Promise\<unknown> | Fetches all entries of a model |
useModelState
Access the current state of a given model.
import { useModelState } from '@rexlabs/model-generator';
import { currentUserModel } from 'models/current-user'
function Component() {
const {name, email} = useModelState(currentUserModel);
...
}
Parameters:
| Name | Type | Description |
|---|---|---|
model | Model |
Returns: ModelDataObject